package com.ruoyi.buss.report.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.buss.report.domain.ReportWgcjBpjmLsb;
import com.ruoyi.buss.utils.DateUtilss;
import com.ruoyi.buss.utils.ListUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.buss.report.mapper.ReportWgshLsbMapper;
import com.ruoyi.buss.report.domain.ReportWgshLsb;
import com.ruoyi.buss.report.service.IReportWgshLsbService;

/**
 * 网格商户报表Service业务层处理
 * 
 * @author sxy
 * @date 2021-07-16
 */
@Service
public class ReportWgshLsbServiceImpl implements IReportWgshLsbService 
{
    @Autowired
    private ReportWgshLsbMapper reportWgshLsbMapper;

    /**
     * 查询网格商户报表
     * 
     * @param sjrq 网格商户报表ID
     * @return 网格商户报表
     */
    @Override
    public ReportWgshLsb selectReportWgshLsbById(String sjrq)
    {
        return reportWgshLsbMapper.selectReportWgshLsbById(sjrq);
    }

    /**
     * 查询网格商户报表列表
     * 
     * @param reportWgshLsb 网格商户报表
     * @return 网格商户报表
     */
    @Override
    public List<ReportWgshLsb> selectReportWgshLsbList(ReportWgshLsb reportWgshLsb, LoginUser userInfo, int type)
    {
        List<ReportWgshLsb> doList = new ArrayList<ReportWgshLsb>();

        String orgId = userInfo.getSysUser().getDept().getOrgId();
        String userId = userInfo.getSysUser().getUserName();
        //如果是admin用户不做处理
        if(userInfo.getRoles().contains("zhhz")){
            reportWgshLsb.setOrgId(orgId);
        } else if(userInfo.getRoles().contains("common")){
            reportWgshLsb.setOrgId(orgId);
            reportWgshLsb.setUserId(userId);
        }

        // 获取上个月最后一天
        reportWgshLsb.setMonthEnd(reportWgshLsb.getSjrq() == null || reportWgshLsb.getSjrq().length()<8 ?
                DateUtilss.getLastDayOfMonth(Integer.valueOf(DateUtils.getMMddStr().substring(0, 2))-1):Integer.valueOf(reportWgshLsb.getSjrq().substring(4,6)) == 1 ?
                Integer.valueOf(reportWgshLsb.getSjrq().substring(0,4))-1+"1231":
                reportWgshLsb.getSjrq().substring(0,4)+DateUtilss.getLastDayOfMonths(Integer.valueOf(reportWgshLsb.getSjrq().substring(4,6))-1));

        // 获取去年最后一天
        reportWgshLsb.setYearEnd(reportWgshLsb.getSjrq() == null || reportWgshLsb.getSjrq().length()<8 ?
                DateUtilss.getLastYear(new Date(), -1):Integer.valueOf(reportWgshLsb.getSjrq().substring(0,4))-1+"1231");

        if (reportWgshLsb.getStartTime() == null) {
            // 如果开始将时间为空 ,则设置开始时间为去年最后一天
            reportWgshLsb.setStartTime(DateUtilss.getLastYear(new Date(), -1));
        }
        if (reportWgshLsb.getEndTime() == null) {
            // 如果结束时间为空则设置结束日期到昨天
            reportWgshLsb.setEndTime(new SimpleDateFormat("yyyyMMdd").format(DateUtilss.getdate(-1)));
        }
        if (!StringUtils.isEmpty(reportWgshLsb.getSjrq())) {
            reportWgshLsb.setSjrq(reportWgshLsb.getSjrq().replace("-", ""));
        }

        if ("".equals(reportWgshLsb.getSjrq())) {
            reportWgshLsb.setSjrq(null);
        }

        if (type == 0) {
            doList = reportWgshLsbMapper.wgshywjdb(reportWgshLsb);

            ReportWgshLsb vo = new ReportWgshLsb();
            List<ReportWgshLsb> list = reportWgshLsbMapper.wgshywjdbhz(reportWgshLsb);
            if(!ListUtil.isNullOrEmpty(list)){
                vo = list.get(0);
                vo.setOrgName("总行汇总数据");
                doList.add(vo);
            }


        } else if (type == 1) {
            ArrayList<String> sjrqs = DateUtilss.getThreeYearFourQuarterAndMonth();
            reportWgshLsb.setSjrqs(sjrqs);
            doList = reportWgshLsbMapper.wgshywjdbMonth(reportWgshLsb);
        } else if (type == 2) {
            ArrayList<String> sjrqs = DateUtilss.getEndMonthByYear(1, 0);
            reportWgshLsb.setSjrqs(sjrqs);
            doList = reportWgshLsbMapper.wgshywjdbMonth(reportWgshLsb);
        }

        return doList;
    }

    /**
     * 新增网格商户报表
     * 
     * @param reportWgshLsb 网格商户报表
     * @return 结果
     */
    @Override
    public int insertReportWgshLsb(ReportWgshLsb reportWgshLsb)
    {
        reportWgshLsb.setCreateTime(DateUtils.getNowDate());
        return reportWgshLsbMapper.insertReportWgshLsb(reportWgshLsb);
    }

    /**
     * 修改网格商户报表
     * 
     * @param reportWgshLsb 网格商户报表
     * @return 结果
     */
    @Override
    public int updateReportWgshLsb(ReportWgshLsb reportWgshLsb)
    {
        return reportWgshLsbMapper.updateReportWgshLsb(reportWgshLsb);
    }

    /**
     * 批量删除网格商户报表
     * 
     * @param sjrqs 需要删除的网格商户报表ID
     * @return 结果
     */
    @Override
    public int deleteReportWgshLsbByIds(String[] sjrqs)
    {
        return reportWgshLsbMapper.deleteReportWgshLsbByIds(sjrqs);
    }

    /**
     * 删除网格商户报表信息
     * 
     * @param sjrq 网格商户报表ID
     * @return 结果
     */
    @Override
    public int deleteReportWgshLsbById(String sjrq)
    {
        return reportWgshLsbMapper.deleteReportWgshLsbById(sjrq);
    }
}
